<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;

class Login extends AdminBase{
    protected function _initialize(){
        //初始化配置
        $this->assignList = [
            'initAdminMenu' => false,//不加载后台菜单
        ];
    }
    /**
     * @name 登陆
     */
    public function login(){
        $this->pageTitle = '登录直播后台管理系统';
        return $this->fetch();
    }
    public function verify(){
        ob_clean();		//清除缓存
        $Verify = new \org\Verify();
        $Verify->fontSize = 25;	//验证码字体大小
        $Verify->length = 4;	//验证码位数
        $Verify->reset = false; //验证成功不重置
        $Verify->entry();
    }
    public function check()
    {
        if(true !== $this->checkData(['account','password','yzcode']))
        {
            return ['status'=>0,'message'=>$this->error_info];
        }
        if(!$this->check_verify($this->yzcode))
        {
            return ['status'=>0,'message'=>'验证码不正确'];
        }
        //验证账号和密码
        $username = string_filter($this->account);   //用户名
        $password = string_filter($this->password);	//密码

        $m = M('admin');
        $user=$m->where(array('username'=>$username))->find();
        if(!$user||hash('sha1', $password.$user['salt'])!=$user['password'])
        {
            return ['status'=>0,'message'=>'账户或密码错误'];
        }
        if(!$user['lock'])
        {   //status为0时表示锁定
            return ['status'=>0,'message'=>'用户被锁定'];
        }else
            {
            $data['login_ip'] = get_client_ip();
            $data['login_time'] = time();
            if (M("admin")->where(array('id' => $user['id']))->save($data))
            {
                M("admin")->where(array('id' => $user['id']))->setInc("login_num");
            }
            //设置登录session
            $aid = encrypt_id($user['id']);
            session('username', $user['username']);//用户名
            session('aid', $aid);//id
            session('is_admin',true);
            session('user_active',NOW_TIME);
            return ['status' => 1, 'data' => ['jumpurl' => U('admin/Index/index')]];
        }
    }
    //安全退出
    public function loginOut()
    {
        session('aid',null);
        session('username',null);
        $data = ['status'=>1,'data'=>['info'=>'安全退成成功','jumpurl'=>U('admin/Login/login')]];
        exit(json_encode($data));
    }
}